package ricelee.demo.rxjava;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private Button integerBtn;
    private Button subscribeBtn;
    private Button threadBtn;
    private Button operatorBtn;

    private TextView contentTv;
    private StringBuilder sBuilder;

    private Toolbar toolbar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        sBuilder = new StringBuilder();
    }

    private void init() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        contentTv = (TextView) findViewById(R.id.tv_content);
        integerBtn = (Button) findViewById(R.id.btn_integer);
        integerBtn.setOnClickListener(this);

        subscribeBtn = (Button) findViewById(R.id.btn_subscribe);
        subscribeBtn.setOnClickListener(this);

        threadBtn = (Button) findViewById(R.id.btn_thread);
        threadBtn.setOnClickListener(this);

        operatorBtn = (Button) findViewById(R.id.btn_operator);
        operatorBtn.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_integer:
                rxObservableInteger();
                break;
            case R.id.btn_subscribe:
                startActivity(new Intent(MainActivity.this, SubscribeActivity.class));
                break;
            case R.id.btn_thread:
                startActivity(new Intent(MainActivity.this, ThreadActivity.class));
                break;
            case R.id.btn_operator:
                startActivity(new Intent(MainActivity.this, OperatorActivity.class));
                break;
        }
    }

    private void rxObservableInteger() {
        //创建一个上游 Observable
        Observable<Integer> observable = Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Integer> emitter) throws Exception {
                emitter.onNext(1);
                emitter.onNext(2);
                emitter.onNext(3);
                emitter.onComplete();
            }
        });
        //创建一个下游 Observer
        Observer<Integer> observer = new Observer<Integer>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {

            }

            @Override
            public void onNext(@NonNull Integer integer) {
                sBuilder.append("---->\t" + integer + "\n");
                contentTv.setText(sBuilder.toString());
            }

            @Override
            public void onError(@NonNull Throwable e) {
                sBuilder.append("---->\t" + "onError");
                contentTv.setText(sBuilder.toString());
            }

            @Override
            public void onComplete() {
                sBuilder.append("---->\t" + "onComplete");
                contentTv.setText(sBuilder.toString());
            }
        };
        observable.subscribe(observer);
    }


}
